<?php defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * @author  Tran Van Thanh
 * @email   thanhtran@vietnambiz.com
 * @date    06.09.2011
 */
 
class Result_model extends MY_Model {
    
    function __construct()
    {
        parent::__construct();
        $_table = $this->db->dbprefix('xs_result');
        $this->_table = $_table;
    }
    
    function get_detect($lid = NULL, $limit = 6)
    {
    	//$rows	=	$this->db->select("CONCAT_WS(',',b0,b1,b2,b3,b4,b5,b6,b7,b8,b9),a0")->from($this->_table)->where('lid',$lid)->order_by('date','DESC')->limit($limit)->get()->result();
    	$query	=	$this->db->query("SELECT replace(CONCAT_WS('',a0,a1,a2,a3,a4,a5,a6,a7,a8),'-','')as data,a0,a1,a2,a3,a4,a5,a6,a7,a8,extension,date FROM ".$this->_table." WHERE lid=".$lid." ORDER BY date DESC LIMIT ".$limit."");
    	//print_r($rows->result());
    	return $query->result();
    }
    
    function get_today()
    {
    	$time	=	date('H:i');
    	$date	=	date('Y-m-d');
    	$location	=	$this->db->dbprefix('xs_location');
    	$arr	=	array(
			'MB'	=>	'Miền Bắc',
			'MT'	=>	'Miền Trung',
			'MN'	=>	'Miền Nam'
		);
		
		$tmp	=	array();
		foreach($arr as $k => $v)
		{
			if($k == 'MB')
			{
				if($time < "19:30")
				{
					$date = date('Y-m-d',strtotime('-1 day'));
				}
			}
			elseif($k == 'MT')
			{
				if($time < "17:30")
				{
					$date = date('Y-m-d',strtotime('-1 day'));
				}
			}
			elseif($k == 'MN')
			{
				if($time < "16:30")
				{
					$date = date('Y-m-d',strtotime('-1 day'));
				}
			}
			$sql	=	"SELECT l.name,l.code, l.alias, l.area,r.* FROM ".$this->_table." r JOIN ".$location." l ON l.id=r.lid WHERE r.id=(SELECT id FROM ".$this->_table." WHERE lid=l.id ORDER BY date DESC LIMIT 1) AND date='".$date."' AND area='".$k."' ORDER BY date DESC";	
    		$rows	=	$this->db->query($sql)->result();
    		$tmp[$k]['result']=$rows;
    		$tmp[$k]['area'] = $v;
			$tmp[$k]['date'] = $date;
		}
		//echo "<pre>";
		//print_r($tmp);
    	return $tmp;
    }
    
}